#include "NetCal.hpp"
#include "Protocol.hpp"
#include "TcpServer.hpp"
#include "Deamon.hpp"
//守护化进程 41

#include <memory>

void Usage(std::string proc)
{
    std::cerr << "Usage: " << proc << " port" << std::endl;
}

int main(int argv, char *argc[])
{
    if (argv != 2)
    {
        Usage(argc[0]);
        exit(USAGE_ERR);
    }
    // std::cout << "服务器已经启动，已经是一个守护进程了" << std::endl;
    // Daemon(0, 0);
    //Daemon(1, 1);

     //Enable_Console_Log_Strategy();
    Enable_File_Log_Strategy();
    // 1. 顶层
    std::unique_ptr<Cal> cal = std::make_unique<Cal>();

    // 2. 协议层
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>([&cal](Request &req)->Response{
        return cal->Execute(req);
    });

    // 3. 服务器层
    std::unique_ptr<TcpServer> tsvr = std::make_unique<TcpServer>(std::stoi(argc[1]), 
        [&protocol](std::shared_ptr<Socket> &sock, InetAddr &client){
            protocol->GetRequest(sock, client);
    });

    tsvr->Start();
    return 0;
}